//
//  CustomProgressView.m
//  circleProgress
//
//  Created by yangyang on 14-11-20.
//  Copyright (c) 2014年 yang. All rights reserved.
//

#import "CustomProgressView.h"
#import "NSString+extention.h"
@interface CustomProgressView ()

@property (nonatomic,strong)CAShapeLayer * trackLayer;
@property (nonatomic,strong)UIBezierPath * trackPath;
@property (nonatomic,strong)CAShapeLayer * progressLayer;
@property (nonatomic,strong)UIBezierPath * progressPath;

@property (nonatomic,strong)UILabel * progressLabel;
@property (nonatomic,strong)UILabel * percentLabel;
@property (nonatomic,strong)UILabel * noteLabel;

@property (nonatomic,assign)CGFloat progressValue;

@end

@implementation CustomProgressView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.trackLayer = [CAShapeLayer new];
        [self.layer addSublayer:self.trackLayer];
        self.trackLayer.fillColor = nil;
        self.trackLayer.frame = self.bounds;
        
        self.progressLayer = [CAShapeLayer new];
        [self.layer addSublayer:self.progressLayer];
        self.progressLayer.fillColor = nil;
        self.progressLayer.lineCap = kCALineCapRound;
        self.progressLayer.frame = self.bounds;
        
    }
    return self;
}
-(void)setTrackLayerWithTrackColor:(CGColorRef)color
                        withRadius:(CGFloat)radius
               withStrokeLineWidth:(CGFloat)tracklineWidth
             withProgressLineWidth:(CGFloat)progressLineWidth
                    withStartAngle:(CGFloat)startAngle
                      withEndAngle:(CGFloat)endAngle{
    self.trackPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:radius startAngle:_degressToRadians(startAngle) endAngle:_degressToRadians(endAngle) clockwise:YES];
    self.trackLayer.lineWidth = tracklineWidth;
    self.trackLayer.strokeColor = color;
    self.trackLayer.path = self.trackPath.CGPath;
}

-(void)setProgressLayerWithProgressColor:(CGColorRef)color
                              withRadius:(CGFloat)radius
                           withLineWidth:(CGFloat)lineWidth
                          withStartAngle:(CGFloat)startAngle
                            withEndAngle:(CGFloat)endAngle
                            withStokeEnd:(CGFloat)progress{
    self.progressPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:radius startAngle:_degressToRadians(startAngle) endAngle:_degressToRadians(endAngle) clockwise:YES];
    self.progressLayer.strokeColor = color;
    self.progressLayer.strokeEnd = progress;
    self.progressLayer.path = self.progressPath.CGPath;
}

-(void)startProgressLayerAnimation:(CGFloat)progress{
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 1.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:progress];
    [self.progressLayer addAnimation:pathAnimation forKey:nil];
}


-(void)addCenterView:(NSInteger)progress{

    NSString * progressString =[NSString stringWithFormat:@"%ld",(long)progress];
    CGSize progressLabelSize = [[NSString alloc] getSizeWithString:progressString WithFontSize:23];
    _progressLabel = [LCFUIGlobel createLabelWithTitle:progressString withFrame:CGRectMake((self.bounds.size.width-progressLabelSize.width)/2-2, 10, progressLabelSize.width, progressLabelSize.height) withFontType:nil withFontSize:23 withFontColor:[UIColor whiteColor] isNeedBold:NO];
    _progressLabel.textAlignment = NSTextAlignmentLeft;
    //progressLabel.backgroundColor = [UIColor blackColor];
    _progressLabel.adjustsFontSizeToFitWidth = YES;
    [self addSubview:_progressLabel];
    
    _percentLabel = [LCFUIGlobel createLabelWithTitle:@"%" withFrame:CGRectMake(_progressLabel.frame.origin.x+_progressLabel.frame.size.width, 20, 13, 13) withFontType:nil withFontSize:10 withFontColor:[UIColor whiteColor] isNeedBold:NO];
    [self addSubview:_percentLabel];

    if(progress != 0&&progress != 100){
        _noteLabel = [LCFUIGlobel createLabelWithTitle:@"完成融资" withFrame:CGRectMake(self.bounds.size.width/2-23, 33, 52, 20) withFontType:nil withFontSize:12 withFontColor:[UIColor colorWithWhite:1 alpha:0.9] isNeedBold:NO];
        [self addSubview:_noteLabel];
        //融资进度
    }else{
        _noteLabel = [LCFUIGlobel createLabelWithTitle:@"即将上线" withFrame:CGRectMake(self.bounds.size.width/2-23, 33, 52, 20) withFontType:nil withFontSize:12 withFontColor:[UIColor colorWithWhite:1 alpha:0.9] isNeedBold:NO];
        [self addSubview:_noteLabel];
    }
    
}



@end
